import logging
from time import sleep

import allure
import selenium
from selenium.webdriver.common.by import By

from pages.basepage import Base
from pages.contactpage import ContactPage


class AddMember(Base):
    _base_url = ""
    # 提取公共变量给多个方法使用，且不对外暴力更多细节，设为私有变量
    # 定位姓名

    _username_locator = (By.CSS_SELECTOR, "#username")
    # 定位账号
    _account_locator = (By.CSS_SELECTOR, "#memberAdd_acctid")
    # 定位手机号
    _phone_locator = (By.CSS_SELECTOR, "#memberAdd_phone")
    # 定位点击按钮
    _savebtn_locator = (By.XPATH, "(//a[contains(@class,'js_btn_save')])[1]")
    # 定位手机号错误信息
    _error_info_locaotor = (By.XPATH, "//*[@id='memberAdd_phone']/../../div[2]")

    @allure.story("添加成员成功")
    def add_member_success(self, username, account_id, phone):
        # 定位姓名
        with allure.step("输入姓名"):
            self.find(self._username_locator).send_keys(username)
        # self.find_and_add_step("输入姓名", self._username_locator).send_keys(username)
        # 定位账号
        with allure.step("输入账号"):
            self.find(self._account_locator).send_keys(account_id)
        # 定位手机号
        with allure.step("输入手机号"):
            self.find(self._phone_locator).send_keys(phone)
        # 点击保存按钮
        with allure.step("点击保存按钮"):
            self.find(self._savebtn_locator).click()
        self.save_png("add member success",name="添加成员成功")
        return ContactPage(self.driver)

    @allure.story("添加成员失败")
    def add_member_fail(self, username, account_id, phone):
        # 定位姓名
        self.find_and_add_step("输入姓名", self._username_locator).send_keys(username)
        # self.find(self._username_locator).send_keys(username)
        # 定位账号
        # self.find(self._account_locator).send_keys(account_id)
        self.find_and_add_step("输入账号",self._account_locator).send_keys(account_id)
        # 定位手机号
        self.find(self._phone_locator).send_keys(phone)
        # 手机号输入框失去焦点
        js = "document.getElementById('memberAdd_phone').blur()"
        self.driver.execute_script(js)
        # 点击保存按钮
        # self.find(self._savebtn_locator)
        sleep(3)
        # 定位错误信息获取报错信息
        error_text = self.find(self._error_info_locaotor)
        # 保存照片
        self.save_png("add member fail",name="添加成员失败")
        # 返回报错的信息
        return error_text.text
